package com.sitech.openapi.controller.customer;

import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import com.sitech.openapi.controller.AbstractController;
import com.sitech.openapi.domain.customer.Customer;
import com.sitech.openapi.domain.user.User;
import com.sitech.openapi.service.UserCmptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Api(tags = "用户信息查询控制层",description = "查询用户相关信息")
@RestController
public class QryUserInfoController extends AbstractController {

    @Autowired
    private UserCmptService userCmptService;
    @Autowired
    private SetHttpHeaderCmpt setHttpHeaderCmpt;

    @GetMapping("/accountSX")
    @ApiOperation(value = "根据号码查询账户信息",notes = "查询账户信息")
    public ResponseEntity<List<Customer>> queryTestByPara(@RequestParam String accNum,
                                                          @RequestParam Integer limit,
                                                          @RequestParam Integer offset,
                                                          @RequestHeader HttpHeaders headers){

        Map<String,Object> map = new HashMap<>();
        map.put("accNum",accNum);
        map.put("pageSize",limit);
        map.put("pageIndex",offset);

        Map<String,Object> resMap = userCmptService.getAccountInfoByAccNum(map);
        List<Customer> customers = (List<Customer>) resMap.get("dataList");
        String total = Long.toString((Long)resMap.get("total"));
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        httpHeaders.set("X-Total-Count",total);
        ResponseEntity<List<Customer>> res = new ResponseEntity<List<Customer>>(customers,httpHeaders, HttpStatus.OK);
        return res;
    }

    @GetMapping("/userSX")
    @ApiOperation(value = "根据客户id或者号码查询user信息",notes = "查询user信息")
    public ResponseEntity<List<User>> queryTestByPara(@RequestParam(required = false) Long custId,
                                                      @RequestParam Integer regionId,
                                                      @RequestParam(required = false) String accNum,
                                                      @RequestParam(required = false) String searchType,
                                                      @RequestParam(required = false) Integer offset,
                                                      @RequestParam(required = false) Integer limit,
                                                      @RequestHeader HttpHeaders headers){

        Map<String,Object> map = new HashMap<>();
        map.put("custId",custId);
        map.put("regionId",regionId);
        map.put("accNum",accNum);
        map.put("searchType",searchType);
        map.put("pageSize",limit);
        map.put("pageIndex",offset);

        Map<String,Object> resMap = userCmptService.getUserInfo(map);
        List<User> users = (List<User>) resMap.get("dataList");
        String total = Long.toString((Long)resMap.get("total"));
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        httpHeaders.set("X-Total-Count",total);
        ResponseEntity<List<User>> res = new ResponseEntity<List<User>>(users,httpHeaders, HttpStatus.OK);
        return res;
    }

    @GetMapping("/prodInst")
    @ApiOperation(value = "查询产品实例信息",notes = "查询产品实例信息")
    public ResponseEntity<List<User>> queryProdInstByPara(@RequestParam(required = false) Long custId,
                                                      @RequestParam(required = false) String accNum,
                                                      @RequestParam(required = false) String account,
                                                      @RequestParam(required = false) String lImsi,
                                                      @RequestParam(required = false) String prodUseType,
                                                      @RequestHeader HttpHeaders headers){
        Map<String,Object> map = new HashMap<>();
        map.put("custId",custId);
        map.put("account",account);
        map.put("accNum",accNum);
        map.put("lImsi",lImsi);
        map.put("prodUseType",prodUseType);

        Map<String,Object> resMap = userCmptService.queryProdInstByPara(map);
        List<User> prodInsts = (List<User>) resMap.get("prodInsts");
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<List<User>> res = new ResponseEntity<List<User>>(prodInsts,httpHeaders, HttpStatus.OK);
        return res;
    }

}
